<?php

namespace app\langjinmobile\controller;

use app\common\model\Business;
use app\common\model\User;
use app\common\model\GoodsCategory;
use app\common\model\Goods;
use app\common\model\Cart;

use think\Controller;
use think\Request;
use think\Validate;

use think\Db;

class Merchant extends Controller
{
    /**
     * 显示资源列表
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 商户入驻显示
     */
    public function apply(){
        $user_id = session('userid');
        //  商家类型
        //$merchant_types = Db::name('shoptype')->all();
        $business_info = Business::where('userid', $user_id)->find();
        return $this->fetch('', ['business_info'=>$business_info]);
    }


    /**
     * 商户入驻逻辑
     */
    public function sendApply(){
        $user_id = session('userid');
        $merchant = Business::where('userid', $user_id)->find();
        $post = Request()->param();
        $validate_res = $this->validate($post, 'app\common\validate\Merchant');

        if(TRUE !== $validate_res){
            return ['code'=>0, 'msg'=>$validate_res];
        }
        $data = $post;
        $data['userid'] = $user_id;

        // 处理上传图片
        if(request()->file('shop_img')){
            $up_res = fileupload(request()->file('shop_img'));
            if(!$up_res || isset($up_res['status'])){
                return ['code'=>0, 'msg'=>$up_res['msg']];
            }
            $data['shop_img'] = $up_res;
        }
        if(request()->file('business_img')){
            $up_res = fileupload(request()->file('business_img'));
            if(!$up_res || isset($up_res['status'])){
                return ['code'=>0, 'msg'=>$up_res['msg']];
            }
            $data['business_img'] = $up_res;
        }

        if($merchant){
            //  更新
            if(!isset($data['shop_img']))
                unset($data['shop_img']);
            if(!isset($data['business_img']))
                unset($data['business_img']);
            Business::where('userid', $user_id)->update($data);
            User::where('id', $user_id)->update(['is_business'=>0]);
            return ['code'=>1, 'msg'=>'入住申请提交成功', 'url'=>'/langjinmobile/my/my'];          
        }
        else{
            if(!isset($data['shop_img']) || !isset($data['business_img'])){
                return ['code'=>0, 'msg'=>'请上传商家图片'];
            }
            Business::create($data);
            return ['code'=>1, 'msg'=>'入住申请提交成功', 'url'=>'/langjinmobile/my/my'];  
        }

    }

    /**
     * 展示添加表单
     *
     * @return     <type>  ( description_of_the_return_value )
     */
    public function showAdd(){
        $categorys = GoodsCategory::where('cat_id','<>',0)->order('parent_id asc')->select();
        return $this->fetch('', ['categorys'=>$categorys]);
    }

    /**
     * 上传商品
     */
    public function doAdd(){
        $uid = session('userid');
        $business = Business::where('userid',$uid)->find();
        if (request()->isPost()) {
            $post = request()->param();
            // print_r($info);exit;
            $validate_res = $this->validate($post, '\app\common\validate\Goods');
            if(TRUE !== $validate_res){
                return ['code'=>0, 'msg'=>$validate_res];
            }
            $validate = Validate::make([
                'name'  => 'unique:goods',
            ], ['name.unique'=>'商品名已经存在']);
            if (!$validate->check($post)) {
                return ['code'=>0, 'msg'=>$validate->getError()];
            }
            if($post['cat_id'] <=0){
                return ['code'=>0, 'msg'=>'商品分类不正确'];
            }
            if(!$business){
                return ['code'=>0, 'msg'=>'商戶信息不存在'];
            }
            $post['img1'] = implode(",",$post['pc_src']);
            unset($post['pc_src']);

            $post['bis_id'] = $business['id'];
            $post['is_on'] = 1;
            Goods::create($post);
            return ['code'=>1, 'msg'=>'添加成功', 'url'=>'/langjinmobile/Merchant/productList'];
        }else{
            exit('请求不合法');
        }
    }


    /**
     * 商家商品列表
     */
    public function productList(){
        $user_id = session('userid');
        $business = Business::where('userid', $user_id)->find();
        $is_showing = Goods::where(['is_on'=>1, 'bis_id'=>$business['id']])->select();
        $is_down = Goods::where(['is_on'=>0, 'bis_id'=>$business['id']])->select();
        foreach($is_showing as $key=>$row){
            $res = explode(',',$row['img1']);
            $is_showing[$key]['img1'] = $res[0];
            
        }
        foreach($is_down as $key=>$row){
            $res = explode(',',$row['img1']);
            $is_down[$key]['img1'] = $res[0];
            
        }
        return $this->fetch('', ['is_showing'=>$is_showing, 'is_down'=>$is_down]);
    }

    /**
     *修改商品
     **/
    public function show_edit(Request $request){
        $uid = session('userid');
        $business = Business::where('userid',$uid)->find();
        $id = $request->param('id');
        $goods = Goods::where(['bis_id'=>$business['id'],'id'=>$id])->find();
        $goods['detail']=html_entity_decode($goods['detail']);
        $goods['img1'] = explode(',',$goods['img1']);
// dump($goods);die;
        $categorys = GoodsCategory::where('cat_id','<>',0)->order('parent_id asc')->select();
        $this->assign('categorys',$categorys);
        //print_r($goods);exit;
        $this->assign('goods',$goods);
        return $this->fetch();
    }

    /**
     *保存商品
     **/
    public  function doEdit(){
        $uid = session('userid');
        $bis_id = Business::where('userid',$uid)->find();
        if (request()->isPost()) {
            $info = $_POST;
            //print_r($info['pc_src']);exit;
            $validate_res = $this->validate($info, '\app\common\validate\Goods');
            if(TRUE !== $validate_res){
                return ['code'=>0, 'msg'=>$validate_res];
            }
            if($info['cat_id'] <=0){
                return ['code'=>0, 'msg'=>'商品分类不正确'];
            }
            $info['img1'] = implode(",",$info['pc_src']);
            unset($info['pc_src']);

            $info['bis_id'] = $bis_id['id'];
            $info['is_on'] = 1;
            // $res = goodsModel::insert($info);
            $res = Goods::where(['id'=> $info['id'],'bis_id'=>$bis_id['id']])->update($info);

            if($res !== FALSE){
                if($info['wallet_num']){
                    $list = Cart::where(['goods_id'=>$info['id'],'status'=>1])->select();
                    foreach($list as $key=>$value){
                        $tprice = $value['num']*$info['wallet_num'];
                        Cart::where(['goods_id'=>$info['id'],'status'=>1,'car_id'=>$value['car_id']])->update(['dprice'=>$info['wallet_num'],'tprice'=>$tprice]);
                    }
                }
                $data['status'] = 1;
                $data['msg'] = '修改成功';
                $data['url'] = '/langjinmobile/Merchant/productList';
            }else{
                $data['status'] = 2;
                $data['msg'] = '修改失败';
            }
            return $data;
        }
    }

    /**
     *下架和上架商品
     **/
    public function check_is_on(Request $request){
        $uid = session('userid');
        $bis_id = Business::where('userid',$uid)->find();
        if($request->isPost()) {
            $id = $request->param('id');
            $res = Goods::where(['bis_id'=>$bis_id['id'],'id'=>$id])->find();
            $info = Goods::get($res['id']);
            //echo $info->getData('is_on');exit;
            $info['is_on'] = $info->getData('is_on');
            if(empty($res)){
                $data['status'] = 2;
                $data['msg'] = lang("商品信息不正确");
                return $data;
            }else{
                if($res['status'] ==1){
                    $data['is_on'] = 0;
                }else{
                    $data['is_on'] = 1;
                }
                $update = Goods::where(['bis_id'=>$bis_id['id'],'id'=>$id])->update($data);
                // echo Goods::getLastSql();exit;
                if($update){
                    $data['status'] = 1;
                    $data['msg'] = lang("提交成功");
                }else{
                    $data['status'] = 2;
                    $data['msg'] = lang("提交失败");
                }
            }
            return $data;
        }
    }

    /**
     * 商家商品列表
     */
    public function upload_img(){

        $file = request()->file('pc_src');
        $up_res = fileupload($file);

        if(!$up_res){
            return ['code'=>0, 'msg'=>'上传出现错误'];
        }
        elseif(isset($up_res['status'])){
            return ['code'=>0, 'msg'=>$up_res['msg']];
        }
        return ['code'=>1, 'filepath'=>$up_res];
    }

    /**
     * 删除商品图片
     **/
    public function del_img(){
        $uid = session('userid');
        $bis_id = Business::where('userid',$uid)->find();
        if (request()->isPost()) {
            $id = $_POST['id'];
            $path = $_POST['path'];
            //echo Env::get('root_path').$path;exit;
            $info = goodsModel::where(['id'=> $id,'bis_id'=>$bis_id['id']])->find();
            if($info){
                unlink(Env::get('root_path').'public/'.$path);
                $data['status'] = 1;
                $data['msg'] = lang("删除成功");
            }else{
                $data['status'] = 2;
                $data['msg'] = lang("删除失败");
            }
            return $data;
        }
    }


}
